package com.zp.resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.order.ResultScheduleGroupVo;
import com.zp.bean.order.ScheduleGroupVo;
import com.zp.bean.user.ResultUserVo;
import com.zp.bean.user.UserVo;
import com.zp.domain.ScheduleGroup;
import com.zp.domain.User;
import com.zp.resource.common.BaseServices;
import com.zp.util.Constant;
import com.zp.util.MyException;

/**
 * schedule group services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/24
 * 
 */
@Component
@Path("/rest/schgroup")
@Singleton
public class ScheduleGroupServices extends BaseServices {
	/**
	 * add schgroup path:/schgroup/addScheduleGroup Http:POST
	 * 
	 * @param schgroup
	 * 
	 * @return Response,ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@POST
	@Path("/addScheduleGroup")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addScheduleGroup(ScheduleGroup schgroup) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			this.jpaDao.save(schgroup);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update ScheduleGroup information path:/schgroup/updateScheduleGroup
	 * Http:PUT
	 * 
	 * @param schgroup
	 * 
	 * @return Response,ResultScheduleGroupVo with JSON String like:
	 */
	@PUT
	@Path("/updateScheduleGroup")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateScheduleGroup(ScheduleGroup schgroup) {
		ResultScheduleGroupVo result = new ResultScheduleGroupVo();
		try {
			this.jpaDao.update(schgroup);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			ScheduleGroupVo vo = new ScheduleGroupVo();
			vo.setId(schgroup.getId());
			vo.setType(this.getTypeByCode(schgroup.getGroupType()));
			try {
				vo.setUser((User) this.jpaDao.findById(User.class,
						schgroup.getUserId()));
			} catch (IllegalArgumentException e) {
				vo.setUser(null);
			}
			result.setSg(vo);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get ScheduleGroup by id path:/type/getScheGroById Http:GET
	 * 
	 * @param id
	 *            primary key of ScheduleGroup
	 * 
	 * @return Response,ResultScheduleGroupVo with JSON String like:
	 */
	@GET
	@Path("/getScheGroById")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getScheduleGroupById(@QueryParam("id") String id) {
		logger.info("getScheduleGroupById called id:" + id);
		ResultScheduleGroupVo result = new ResultScheduleGroupVo();
		try {
			ScheduleGroup schgroup = (ScheduleGroup) jpaDao.findById(
					ScheduleGroup.class, id);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			ScheduleGroupVo vo = new ScheduleGroupVo();
			vo.setId(schgroup.getId());
			vo.setType(this.getTypeByCode(schgroup.getGroupType()));
			try {
				vo.setUser((User) this.jpaDao.findById(User.class,
						schgroup.getUserId()));
			} catch (IllegalArgumentException e) {
				vo.setUser(null);
			}
			result.setSg(vo);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get ScheduleGroup by user id path:/type/getScheGroByUser Http:GET
	 * 
	 * @param userId
	 *            primary key of user
	 * 
	 * @return Response,ResultScheduleGroupVo with JSON String like:
	 */
	@GET
	@Path("/getScheGroByUser")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getScheduleGroupByUserId(@QueryParam("userId") String userId) {
		logger.info("getScheduleGroupById called userId:" + userId);
		ResultScheduleGroupVo result = new ResultScheduleGroupVo();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		String jpql = "from ScheduleGroup where userId=?";
		Object[] queryParams = { userId };

		try {
			List<?> list = this.jpaDao.getResultData(Constant.NEGATIVE_ONE,
					Constant.NEGATIVE_ONE, jpql, queryParams);
			if (list != null && list.size() > Constant.ZERO) {
				ScheduleGroup schgroup = (ScheduleGroup) list
						.get(Constant.ZERO);
				ScheduleGroupVo vo = new ScheduleGroupVo();
				vo.setId(schgroup.getId());
				vo.setType(this.getTypeByCode(schgroup.getGroupType()));
				try {
					vo.setUser((User) this.jpaDao.findById(User.class,
							schgroup.getUserId()));
				} catch (IllegalArgumentException e) {
					vo.setUser(null);
				}
				result.setSg(vo);
			}
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete ScheduleGroup by id path:/schgroup/deleteScheduleGroup Http:DELETE
	 * 
	 * @param id
	 *            primary key of ScheduleGroup
	 * 
	 * @return Response,ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@DELETE
	@Path("/deleteScheduleGroup")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteScheduleGroup(@QueryParam("id") String id) {
		logger.info("deleteScheduleGroup called id:" + id);
		ResultCommonVo result = new ResultCommonVo();
		try {
			this.jpaDao.deleteById(ScheduleGroup.class, id);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * search all of the users with one group path:/schgroup/searchScheduleGroup
	 * Http:GET
	 * 
	 * @param groupType
	 *            the code of schedule group type
	 * 
	 * @return Response, ResultScheduleGroupVo with JSON String like:
	 */
	@GET
	@Path("/searchScheduleGroup")
	@Produces(MediaType.APPLICATION_JSON)
	public Response searchScheduleGroupWithType(
			@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("groupType") String groupType) {
		logger.info("searchScheduleGroup called PageNo is:" + pageNo
				+ " pageSize is:" + pageSize + " json groupType:" + groupType);
		String jpql = "from ScheduleGroup where groupType=?";
		List<?> list = null;
		ResultUserVo result = new ResultUserVo();
		List<UserVo> lt = new ArrayList<UserVo>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		Object[] param = { groupType };
		try {
			list = this.jpaDao.getResultData(pageNo, pageSize, jpql, param);
			long totalCount = this.jpaDao.getCount(
					"select count(id) from ScheduleGroup where groupType=?",
					param);
			result.setTotalCount(totalCount);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				ScheduleGroup schgroup = (ScheduleGroup) o;
				UserVo vo = new UserVo();
				User user;
				try {
					user = (User) this.jpaDao.findById(User.class,
							schgroup.getUserId());
					vo.setBirthday(user.getBirthday());
					vo.setCertNumber(user.getGender());
					vo.setGender(user.getGender());
					vo.setId(user.getId());
					vo.setLogin(null);
					vo.setName(user.getName());
					vo.setRegisterTime(user.getRegisterTime());
					vo.setType(null);
					vo.setUpdateTime(user.getUpdateTime());
					lt.add(vo);
				} catch (MyException e) {
					e.printStackTrace();
				}

			}
			result.setListVo(lt);

		}
		return Response.ok(result).build();
	}

}
